from Interpolation import Interpolation, run

class Newton(Interpolation):
    def __init__(self):
        super().__init__()
        self.divdiff = []
    def addKey(self, x, y):
        super().addKey(x, y)
        thisRow = [y]
        if len(self.divdiff)>0:
            lastRow = self.divdiff[-1]
            thisY = y
            for i, y in enumerate(lastRow):
                div = x-self.keys[len(lastRow)-1-i].x
                thisY = (thisY-y)/div
                thisRow.append(thisY)
        self.divdiff.append(thisRow)
        print(thisRow)
        def fn(x):
            sum = 0
            for i in range(len(self.keys)):
                prod = 1
                for j in range(i):
                    prod *= (x-self.keys[j].x)
                sum += (prod * self.divdiff[i][i])
            return sum
        self.compute = fn

run(Newton())